﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Controller;
using System.Runtime.InteropServices;

namespace QLKS
{
    public partial class FrmAddEmployee : Form
    {
        #region Khai báo biến
        private byte[] buff = null;
        private string uriImage = null;
        string employeeID =null;
        string name = null;
        int sex =0;
        string birthDay =null;
        string personID =null;
        string dateOfIssue =null;
        string placeOfIssue =null;
        string hometown = null;
        string email = null;
        string mobile = null;
        string addressE = null;
        string salary = null;
        string homePhone = null;

        private string IDEmployee { get; set; }
        public delegate void SendInfo(string id);
        public SendInfo sender;
        DataTable dt = new DataTable();

        Controller_Employee cont_employ = new Controller_Employee();
        #endregion
        public FrmAddEmployee()
        {
            InitializeComponent();
            sender = new SendInfo(getInfo);
        }

        private void getInfo(string id)
        {
            if(id != null && id.Length != 0){
                dt = cont_employ.SelectOne_Employee(id);
                txtMaNV.Text = dt.Rows[0][0].ToString();
                txtHoTen.Text = dt.Rows[0][1].ToString();
                string gt = dt.Rows[0][2].ToString().ToLower().Trim() ;
                if (gt.CompareTo("true") == 0)
                    radNam.Checked = true;
                else if (gt.CompareTo("false") == 0)
                    radNu.Checked = true;
                dtp_NgaySinh.Text = dt.Rows[0][3].ToString();
                txtCMND.Text = dt.Rows[0][4].ToString();
                dtp_NgayCap.Text = dt.Rows[0][5].ToString();
                txtNoiCap.Text = dt.Rows[0][6].ToString();
                txtNguyenQuan.Text = dt.Rows[0][7].ToString();
                txtEmail.Text = dt.Rows[0][8].ToString();
                txtDiDong.Text = dt.Rows[0][9].ToString();
                txtDiaChi.Text = dt.Rows[0][10].ToString();
                txtLuong.Text = dt.Rows[0][11].ToString();
                // hình            
                if (dt.Rows[0][12].ToString() != "")
                {
                    buff = (byte[])dt.Rows[0][12];
                    if (buff.Length != 0) // nếu ảnh khác rỗng
                    {
                        MemoryStream stream = new MemoryStream(buff);
                        pic_NhanVien.Image = Image.FromStream(stream);
                    }
                }
                txtDTNha.Text =dt.Rows[0][13].ToString();       

            }

        }

        //#region Xử lí không cho di chuyển form
        //[DllImport("user32.dll")]
        //private static extern Int32 EnableMenuItem(System.IntPtr hMenu, Int32 uIDEnableItem, Int32 uEnable);
        //private const Int32 HTCAPTION = 0x00000002;
        //private const Int32 MF_BYCOMMAND = 0x00000000;
        //private const Int32 MF_ENABLED = 0x00000000;
        //private const Int32 MF_GRAYED = 0x00000001;
        //private const Int32 MF_DISABLED = 0x00000002;
        //private const Int32 SC_MOVE = 0xF010;
        //private const Int32 WM_NCLBUTTONDOWN = 0xA1;
        //private const Int32 WM_SYSCOMMAND = 0x112;
        //private const Int32 WM_INITMENUPOPUP = 0x117;

        //protected override void WndProc(ref System.Windows.Forms.Message m)
        //{
        //    if (m.Msg == WM_INITMENUPOPUP)
        //    {
        //        //thu ly hien thi menu he thong
        //        if ((m.LParam.ToInt32() / 65536) != 0) // 'chia cho 65536 de lay hiword
        //        {
        //            Int32 AbleFlags = MF_ENABLED;
        //            AbleFlags = MF_DISABLED | MF_GRAYED; // ko cho di chuyen

        //            EnableMenuItem(m.WParam, SC_MOVE, MF_BYCOMMAND | AbleFlags);
        //        }
        //    }

        //    if (m.Msg == WM_NCLBUTTONDOWN) //chan viec di chuyen titlebar
        //    {
        //        if (m.WParam.ToInt32() == HTCAPTION) return;
        //    }
        //    if (m.Msg == WM_SYSCOMMAND) // ngan viec di chuyen tren menu he thong
        //    {
        //        if ((m.WParam.ToInt32() & 0xFFF0) == SC_MOVE) return;
        //    }

        //    base.WndProc(ref m);
        //}
        //#endregion
        private void btn_Duyet_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();           
            dlg.InitialDirectory = @"E:\Hinh nen\3D_Animal_Wall";
            dlg.Filter = "“JPG Files(*.JPG)|*.JPG|GIF Files(*.GIF)|*.GIF";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                uriImage = dlg.FileName;
                FileStream fs = new FileStream(uriImage, FileMode.Open);
                //Tao mot mang byte[] bang voi FileStream de doc FileStream fs
                buff = new byte[fs.Length];
                //Doc FileStream fs vua tao o tren vao mang byte[] buff
                fs.Read(buff, 0, (int)fs.Length);
                //Hien thi anh
                pic_NhanVien.Image = Image.FromStream(fs);
                fs.Close();
            }
                
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {

            pic_NhanVien.Image = null;

        }
        public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

            // Prevent using images internal thumbnail
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (OnlyResizeIfWider)
            {
                if (FullsizeImage.Width <= NewWidth)
                {
                    NewWidth = FullsizeImage.Width;
                }
            }

            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                // Resize with height instead
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }

            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

            // Clear handle to original file so that we can overwrite it if necessary
            FullsizeImage.Dispose();

            // Save resized picture
            NewImage.Save(NewFile);
        }
        
        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (doCheckInfo() == false) return;
            employeeID = txtMaNV.Text;
            name = txtHoTen.Text;
            if (radNam.Checked == true)
                sex = 1;
            else if (radNu.Checked == true)
                sex = 0;
            birthDay = dtp_NgaySinh.Value.ToShortDateString();
            personID = txtCMND.Text;
            dateOfIssue = dtp_NgayCap.Value.ToShortDateString();
            placeOfIssue = txtNoiCap.Text;
            hometown = txtNguyenQuan.Text;
            homePhone = (txtDTNha.Text.Trim() == "") ? "0" : txtDTNha.Text;
            mobile = txtDiDong.Text;
            email = txtEmail.Text;
            addressE = txtDiaChi.Text;
            salary = txtLuong.Text;

            string startupPath = Application.StartupPath;
            if (cont_employ.Insert_employee(employeeID, name, sex, birthDay, personID, dateOfIssue, placeOfIssue, hometown, email, mobile, addressE, salary, buff, homePhone, startupPath))
            {
                MessageBox.Show("Thêm Nhân Viên thành công","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else
                MessageBox.Show("Không thể thêm NV mới");
            
        }

        private bool doCheckInfo()
        {
            if (txtMaNV.Text.Trim() == "")
            {
                txtMaNV.Focus();
                MessageBox.Show("Nhập thiếu Mã nhân viên","Nhập sai",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return false;
            }
            if (txtHoTen.Text.Trim() == "")
            {
                txtHoTen.Focus();
                MessageBox.Show("Nhập thiếu Họ tên", "Nhập sai", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtCMND.Text.Trim() == "")
            {
                txtCMND.Focus();
                MessageBox.Show("Nhập thiếu số CMND", "Nhập sai", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtNoiCap.Text.Trim() == "")
            {
                txtNoiCap.Focus();
                MessageBox.Show("Nhập thiếu Nơi cấp", "Nhập sai", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtNguyenQuan.Text.Trim() == "")
            {
                txtNguyenQuan.Focus();
                MessageBox.Show("Nhập thiếu Quê quán", "Nhập sai", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if(txtDiDong.Text.Trim()=="")
            {
                txtDiDong.Focus();
                MessageBox.Show("Nhập thiếu Số di động", "Nhập sai", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtLuong.Text.Trim() == "" || float.Parse(txtLuong.Text) == 0)
            {
                txtLuong.Focus();
                MessageBox.Show("Nhập sai Lương", "Nhập sai", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void FrmAddEmployee_Load(object sender, EventArgs e)
        {
            if (txtMaNV.Text.Length != 0)
            {
                txtMaNV.Enabled = false;
                btnSua.Enabled = true;
            }

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            employeeID = txtMaNV.Text;
            name = txtHoTen.Text;
            if (radNam.Checked == true)
                sex = 1;
            else if (radNu.Checked == true)
                sex = 0;
            birthDay = dtp_NgaySinh.Value.ToShortDateString();
            personID = txtCMND.Text;
            dateOfIssue = dtp_NgayCap.Value.ToShortDateString();
            placeOfIssue = txtNoiCap.Text;
            hometown = txtNguyenQuan.Text;
            homePhone = txtDTNha.Text;
            mobile = txtDiDong.Text;
            email = txtEmail.Text;
            addressE = txtDiaChi.Text;
            salary = txtLuong.Text;
            string startupPath = Application.StartupPath;

            if (cont_employ.Update_Employee(employeeID, name, sex, birthDay, personID, dateOfIssue, placeOfIssue, hometown, email, mobile, addressE, salary, buff, homePhone, startupPath))
            {
                MessageBox.Show("Sửa thông tin Nhân viên thành công", "Thông báo");
                FrmInfoEmployee obj = new FrmInfoEmployee();
                foreach (Form f in MdiChildren)
                {
                    if (f is FrmInfoEmployee)
                    {
                        f.Activate();
                        return;
                    }
                }
                FrmInfoEmployee infoE = new FrmInfoEmployee();
                obj.send();
                infoE.MdiParent = MdiParent;
                infoE.Show();
                
            }
            else
                MessageBox.Show("Không sửa được", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
                e.Handled = true;
        }
    }
}
